package com.qn.controller;

import com.qn.entity.DueOutMail;
import com.qn.entity.MailCarrier;
import com.qn.entity.Sendnumbers;
import com.qn.service.CompanyDataService;
import com.qn.service.MailPushService;
import com.qn.utils.Msg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Queue;

/**
 * @author olo
 */
@RestController
@RequestMapping("/emailPushModule")
public class EmailPushController {
    @Resource
    private MailPushService mailPushService;
    @Resource
    private CompanyDataService companyDataService;

    /**
     * 群发邮件
     *
     * @param mailCarrier 邮件抄送载体
     */
    @PostMapping("MassSendMail")
    public void massSendMail(@RequestBody MailCarrier mailCarrier) {
        mailPushService.pushSendQueue(new DueOutMail(companyDataService.findAllEmail(mailCarrier.getMyPageInfo()), mailCarrier.getMailTitle(), mailCarrier.getMailContent()));
    }

    /**
     * 获得队列状态
     *
     * @return java.util.Queue<com.qn.entity.DueOutMail>
     * @description TODO
     * @author LinXiPeng
     * @date 2021/10/24
     */
    @GetMapping("getQueueStatus")
    public Queue<DueOutMail> getQueueStatus() {
        return mailPushService.getMailSendQueue();
    }

    @GetMapping("getSenders")
    public Msg getSenders(Integer id) {
        return Msg.success().add("senders", mailPushService.getSendEmails(id));
    }

    @DeleteMapping("deleteThisSender")
    public boolean deleteThisSender(Integer id) {
        return mailPushService.deleteThisSender(id) == 1;
    }

    @PostMapping("addOneSender")
    public Msg addOneSender(@RequestBody Sendnumbers sendnumbers) {
        return mailPushService.addOneSender(sendnumbers) ? Msg.success() : Msg.fail();
    }

    @PutMapping("editThisSender")
    public Msg editThisSender(@RequestBody Sendnumbers sendnumbers) {
        return mailPushService.editThisSender(sendnumbers) ? Msg.success() : Msg.fail();
    }
}
